$(document).ready(function(){


   $("#addProductForm").validate({

        rules: {      
            url: { required: true, url: true, maxlength: 300},   
            vcode: {required: true}
         },
         messages: {
            url: { required:"链接不能为空", url:"链接格式不正确", maxlength: "最大长度不超过300字符"},       
            vcode: {required:"验证码不能为空，请输入验证码"}             
         },
         submitHandler: function(form) {
        	 return true;
            //form.submit();
         }
     });
     
   $("#addProductForm2").validate({
	    
	        rules: {      
	            productName: { required: true, maxlength: 200},   
	            productPrice: {required: true, number:true, maxlength: 10},
	            productPicture: {maxlength: 200},
	            selcity: {required: true},
	            productCategory: {required: true}
	            
	         },
	         messages: {
	            productName: { required:"商品名称不能为空", maxlength: "最大长度不超过200字符"},   
	            productPrice: { required:"商品价格不能为空",number:"格式不正确，请输入数字", maxlength: "最大长度不超过10字符"},  
	            productPicture: { maxlength: "最大长度不超过200字符"},  
	            selcity: {required:"所在地不能为空"},
	            productCategory: {required:"类别不能为空"}     
	         },
	         submitHandler: function(form) {
	            return true;
	         }
     });

     
     if($("#province").length>0)
    	 LoadLocation($("#province").val(),$("#city").val());     
    	 
     if($("#pCateName").length>0)
    	 LoadCate($("#pCateName").val());           
});

 function refresh_vcode(url){
 
   $("#vimg").attr('src',url);
 
 }
 
 function previewLogo(){
   	 $("#product_pre_pic").attr('src',$("#productPicture").val());
}

 $(document).ready(function(){
	$("#productDes img").each(function(){
		var width = 550;
		var image = $(this);
		
	    if(image.width()>width){
	     image.width(width);
	     image.height(width/image.width()*image.height());
	    }   
	});
});	
 
 function previewIntro(){
		
		// the unblockUI plugin can't handle the iframe correctly, so
		// we have to create the block dialogue ourself here.
		$.blockUI("");
		// $("#productDesc").val().replace(/\n/g, "<br/>")
		$("#previewFrame").contents().find("body").html( $("#productDesc").val() );
		$("#previewFrame").css("border" , "none" );
		
		// make it located in the center of the window
		$("#previewWindow").width("800px");
		$("#previewWindow").height( "500px" );
		$("#previewWindow").css("margin-left" , "-400px");
		$("#previewWindow").css( "margin-top", "-250px" );
		$("#previewWindow").css( "display", "block");
		$("#previewWindow").css( "z-index", "9999");
		$("#previewWindow").css("position" , "absolute");
		$("#previewWindow").css("left" , "50%" );
		$("#previewWindow").css("top" , "50%" );
		$("#previewWindow").css("background" , "white" );
		
		
		
	}

function closePreview(){
	UnblockUI(100);
	$("#previewWindow").css( "display", "none");
}

function LoadLocation( province, city ){

	var options = $("#selprovince" ).html();
	for ( var key in cityMap ){
		if( key == province )
			options += "<option value='" + key + "' selected>" +  key +"</option>";
		else
			options += "<option value='" + key + "'>" +  key +"</option>";
	}
	
	$("#selprovince").html( options );
	
	$("#selprovince").change(
		function()
		{
			var citys = "<option value=''>选择城市</option>";
			var pro = $("#selprovince option:selected").get(0).value;

			if( pro != '' ){
				for( i = 0; i < cityMap[pro].length; i++ )
				{
					citys += "<option value='" + cityMap[pro][i] + "'>" + cityMap[pro][i] + "</option>";
				}
				$("#selcity").html( citys );
				$("#province").val( pro );
			}
			else{
				$("#selcity").html( citys );
			}
		} 
		);
	$("#selcity").change(
		function()
		{
			$("#city").val( $("#selcity option:selected").get(0).value );
		}
	);

	//init selection
	var curprovince = $("#selprovince option:selected").get(0).value;
	if( curprovince != '' ){
		var citys = "";
		var pro = $("#selprovince option:selected").get(0).value;
		for( i = 0; i < cityMap[pro].length; i++ )
		{
			if( city == cityMap[pro][i] )
				citys += "<option value='" + cityMap[pro][i] + "' selected>" + cityMap[pro][i] + "</option>";
			else
				citys += "<option value='" + cityMap[pro][i] + "'>" + cityMap[pro][i] + "</option>";
		}
		$("#selcity").html( citys );
	}
	//default selection
	else{
		$("#selprovince").val('0');
		$("#selprovince").change();
	}
}

function LoadCate( cateName ){

	//找到商品大类
	var cateNameF = "";
	
	for ( var pCate1 in productCateMap ){
		
		for( i = 0; i < productCateMap[pCate1].length; i++ )
		{
			if( cateName == productCateMap[pCate1][i] ){
				cateNameF = pCate1;
				break;
			}
		}			
		if(cateNameF == pCate1)
			break;
	}
	
	//加载商品大类
	var CateOptionsF =$("#productCategoryF").html();	
	for ( var key in productCateMap ){
		if( key == cateNameF )
			CateOptionsF += "<option value='" + key + "' selected>" +  key +"</option>";
		else
			CateOptionsF += "<option value='" + key + "'>" +  key +"</option>";
	}
	$("#productCategoryF").html( CateOptionsF );	
	
	$("#productCategoryF").change(
		function()
		{
			var CateOptions = "<option value=''>选择商品分类</option>";
			var cateNameF = $("#productCategoryF option:selected").get(0).value;		
			for( i = 0; i < productCateMap[cateNameF].length; i++ )
			{
				CateOptions += "<option value='" + productCateMap[cateNameF][i] + "'>" + productCateMap[cateNameF][i] + "</option>";
			}
			$("#productCategory").html( CateOptions );
	
	});
	
	if( cateNameF != '' ){
		//加载商品小类
		var CateOptions =$("#productCategory").html();	
		for( i = 0; i < productCateMap[cateNameF].length; i++ ){
			if( cateName == productCateMap[cateNameF][i] )
					CateOptions += "<option value='" + productCateMap[cateNameF][i] + "' selected>" + productCateMap[cateNameF][i] + "</option>";
				else
					CateOptions += "<option value='" + productCateMap[cateNameF][i] + "'>" + productCateMap[cateNameF][i] + "</option>";
		
		}
		$("#productCategory").html( CateOptions );		
	
	}

}
